//index.js
//获取应用实例
var common = require('../../utils/common.js')
var util = require('../../utils/util.js')
var config = require('../../config.js')
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    bannerList: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    isHideLoadMore: true,
    articleMain: [],
    currentPage: 1,
    offline:false,
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    const that=this;
    wx.getNetworkType({
      success: function (res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        var networkType = res.networkType
        if (networkType=="none"){
         that.setData({
           offline:true
         })
         wx.hideNavigationBarLoading() //完成停止加载
         wx.stopPullDownRefresh() //停止下拉刷新
        }
        else{
          that.setData({
            offline: false
          })
          /****获取首页轮播图 *****/
          that.getBanner();
          /****获取首页推荐列表****/
          that.getAtrticleMain(true);
        }
      }
    })
  

  },
  onReady: function () {//页面ready
    //获得dialog组件
    this.loadmore = this.selectComponent("#loadmore");
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onPullDownRefresh: function () {//上下拉刷新加载
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载
    this.setData({
      currentPage: 1
    })
    this.onLoad()
    // this.getAtrticleMain(true);
  }, onReachBottom: function (e) {//上拉
    this.loadmore.show();
    this.getBanner();
    const pagenum = this.data.currentPage + 1
    this.setData({
      currentPage: pagenum
    })
    this.getAtrticleMain(false);
    // this.loadmore.hide();
  },
  getAtrticleMain: function (isRefresh) {//获取首页推荐列表
    const that = this;
    var pagenum = this.data.currentPage;
    var articleMain = this.data.articleMain;
    const url = "/apid/r/100/" + pagenum;
    util.DoCommand(url, null, null, function (data) {
      var pagenum = that.data.currentPage;
      if (data.length < app.globalData.pagesize && pagenum != 1) {
        pagenum = pagenum - 1;
      }
      if (isRefresh == true) {
        articleMain = data
      }
      else {
        //合并数据
        for (var i in data) {
          var _id = data[i].id;
          var hasCount = 0;//标识是否已存在
          for (var j in articleMain) {
            var _nid = articleMain[j].id;
            if (_nid == _id) {
              hasCount++;
            }
          }
          //如果不存在则追加数据
          if (hasCount < 1) {
            articleMain.push(data[i]);
          }
        }
      }
      that.setData({
        articleMain: articleMain,
        currentPage: pagenum
      })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
       that.loadmore.hide();
    }, null, that)
  },
  getBanner: function () {//获取首页轮播图列表
    const that = this;
    const url = "/apid/r/101";
    util.DoCommand(url, null, null, function (data) {
      that.setData({
        bannerList: data
      })
    },null,that)
  },
  bannerClick: function (e) {//点轮播图触发事件
    const currentIndex = e.currentTarget.dataset.index;
    const currentData = this.data.bannerList[currentIndex];
    //如果为文章类型，则跳转文章详情
    if (currentData.type == "article" && currentData.entityid != '') {
      var articleid = e.currentTarget.dataset.articleid;
      wx.navigateTo({
        url: '../../pages/articledetail/articledetail?id=' + articleid
      })
    }
    else {
      //其他类型链接，待定 TODO
    }
  },
  tapArticle: function (e) {//点击文章
    //获取点击id
    const that=this;
    var articleid = e.currentTarget.dataset.articleid;
    const newid = util.NewGuid();
    const url = "/apid/c/144";
    const accountid = app.globalData.accountid;
    const params = "articleid=" + articleid + "&accountid=" + accountid + "&id=" + newid
    //调用接口记录阅读状态
    util.DoCommand(url, params, null, function (data) { }, null, that)
    wx.navigateTo({
      url: '../../pages/articledetail/articledetail?id=' + articleid
    })
  },
  //自测数据 登记
  measureRegister:function(e){
    const patientid = app.globalData.patientid;
    if(patientid!=''){
      wx.navigateTo({
        url: '../../pages/measureregister/measureRegister'
      })
    }
    else{
      util.NotVip();
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
